package com.wandian.demo
{
	import avmplus.getQualifiedClassName;

	import com.wandian.ApplicationFacade;
	import com.wandian.data.GameData;
	import com.wandian.model.vo.PlayerVo;
	import com.wandian.util.CLZButton;
	import com.wandian.util.CLZSlide;
	import com.wandian.util.Res;
	import com.wandian.util.StopWatch;
	import com.wandian.util.UIHelper;
	import com.wandian.view.components.ui.CardCount;

	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.utils.ByteArray;
	import flash.utils.describeType;
	import flash.utils.getDefinitionByName;

	/**
	 */
	public class Main extends Sprite
	{
		public var facade:ApplicationFacade;
		private var registPanel:RegistPanel;

		private var client:NetClient;
		private var tf:TextField=new TextField();

		public function Main()
		{
//			testStopTime();
			/*stage.scaleMode = "noScale";

			client = new NetClient();
			client.buildConnection();
			client.addEventListener(NetEvent.READED_DATA, onReadedDataHandler);
			client.addEventListener(Event.CONNECT, onConnectServerHandler);

			registPanel = new RegistPanel();
			addChild(registPanel);
			registPanel.addEventListener(NetEvent.NET_CONNECT, requestConnectServer);

			tf.border = true;
			tf.width = 300;
			tf.height = 400;
			tf.x=50;
			tf.y = 50;
			tf.type = TextFieldType.INPUT;
			addChild(tf);*/
//			testWriteObject();
//			testReadObject(testWriteObject());
//			testRoomCode();
//			testLoad();
//			testCopy();
//			chatRoom();
//			testLoad();
			testCardType();
		}

		private var cardTypes:String="高牌|一对|两对|三条|顺子|同花|葫芦|四条|同花顺|皇家同花顺";
		var arrCardType:Array;

		private function testCardType():void
		{
			var arrCards:Array=["1h", "2h", "3h", "4h", "5h"];
			arrCardType=cardTypes.split("|");
			showCardTip(arrCards, 8);
			arrCards=["Jd", "7s", "5h", "2d", "6s"];
			showCardTip(arrCards, 0);
			arrCards=["8c", "8h", "7s", "Jd", "4h"];//type,葫芦?
			showCardTip(arrCards, 1);
			arrCards=["Ts", "Th", "2d", "2c", "7s"];
			showCardTip(arrCards, 2);
			arrCards=["8d", "8c", "8h", "Js", "Ts"];
			showCardTip(arrCards, 3);
			arrCards=["4d", "5c", "6h", "7s", "8s"];
			showCardTip(arrCards, 4);
			arrCards=["3h", "7h", "Th", "5h", "Kh"];
			showCardTip(arrCards, 5);
			arrCards=["8d", "8c", "8h", "Jc", "Jh"];
			showCardTip(arrCards, 6);
			arrCards=["6d", "6c", "6h", "6s", "4d"];
			showCardTip(arrCards, 7);
			arrCards=["Ks", "Qs", "Js", "Ts", "9s"];
			showCardTip(arrCards, 8);
			arrCards=["As", "Ks", "Qs", "Js", "Ts"];
			showCardTip(arrCards, 9);
		}

		private function showCardTip(arrCards:Array, type:int):void
		{

			trace("下面的1类型应该是1：" + arrCardType[type]);
			var cardType:Object=null;
			cardType=CardCount.instance().countCardType(arrCards);
			// {type: 5, arr1: copySameColor, huase: huaseIndex}
			trace(arrCards + "：type," + arrCardType[cardType.type] + " arr1," + cardType.arr1 + " huase," + cardType.huase);
//			this.sendNotification(CLZNoticeName.SHOW_CARD_TIP, {seatId: playerVo.seatId, msg: arrCardType[cardType.type]});




			return;
		}


		var viewComponent:Object;

		public function testLoad()
		{
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onResLoadComplete);
			var context:LoaderContext=new LoaderContext(false, ApplicationDomain.currentDomain);
			loader.load(new URLRequest("view/ui/Res.swf"), context);

		}

		private function get mcView():MovieClip
		{
			return viewComponent as MovieClip;
		}

		private function onResLoadComplete(e:Event):void
		{
			//初始化玩资源文件，就开始初始化定义样式
			Res.init(null);
			testSetRoom();

		}

		private function clickSetRoomHandler(event:MouseEvent):void
		{

		}
		private var _slide:CLZSlide;

		private function testSetRoom():void
		{
			var mc:MovieClip=Res.getMC("SetRoomPanel");
			this.viewComponent=mc;
			GameData.stopMcAllChild(mc);
			addChild(mc);
			this.mcView.title_txt.text="房间设置";
			this.mcView.txt_roomseat.htmlText="2222222";
			this.mcView.btn_ok.addEventListener(MouseEvent.CLICK, this.clickSetRoomHandler);
			CLZButton.setButton(mc.btn_ok, " 确定设置");
			this._slide=new CLZSlide(this.mcView.slide_mc.btn, this.mcView.slide_mc.bg, this.mcView.slide_mc.sub_btn, this.mcView.slide_mc.add_btn);
			this._slide.width=this.mcView.slide_mc.bg.width - this.mcView.slide_mc.btn.width;
			this._slide.addEventListener(Event.CHANGE, this.changeSlideHandler);
			this._slide.setMinMax(3, 9, 1, 1);
		}

		private function changeSlideHandler(event:Event):void
		{
//			CLZButton.updateMcText(, "加注" + " " + this._slide._value);
			this.mcView.txt_roomseat.text=this._slide._value;
//			this.mcView.txt_roomseat.htmlText=this._slide._value;
			return;
		}

		private function chatRoom():void
		{

		}

		private function testCopy():void
		{
			var from:PlayerVo=new PlayerVo();
			from.userid=111;
			from.lastAction=00;
			from.name="from";
			var to:PlayerVo=new PlayerVo();
			to.userid=222;
			to.name="to";
//			var reflect:TypeDescription=describeType(from);
//			reflect.Copy(from, to);
			copyClass(from, to);
			trace(to.userid + " " + to.name);
//			reflect.traceInfo();
//			tr
//			copyClass(from, to);
		}

		public function copyClass(fromeVO:Object, toVO:Object):Object
		{
			var prop:String;
			var xml:*=describeType(toVO);
			var propertyNames:Array=[];
			var element:*=xml.typeInfo.factory.variable;
			var index:int=0;
			while (index < element.length())
			{
				propertyNames.push(element[index].@name.toString());
				index=index + 1;
			}
			index=0;
			while (index < propertyNames.length)
			{
				prop=propertyNames[index];
				try
				{
					if (fromeVO.hasOwnProperty(prop))
					{
						toVO[prop]=fromeVO[prop];
					}
				}
				catch (e:Error)
				{
					trace("copyClass Error:" + e);
				}
				index++;
			}
			return toVO;

		}

		public function copyClass1(param1:Object, param2:Object):Object
		{
			var variableXML:XML;
			var accessorXML:XML;
			var prop:String;
			var fromeVO:*=param1;
			var toVO:*=param2;
			var xml:*=describeType(toVO);
			var propertyNames:Array;
			var _loc_4:int=0;
			var _loc_5:*=xml.variable;
			while (_loc_5 in _loc_4)
			{

				variableXML=_loc_5[_loc_4];
				propertyNames.push(variableXML.@name);
			}
			var _loc_4:int=0;
			var _loc_5:*=xml.accessor;

			for each (var propertyInfo:XML in _loc_5)
			{

				// 取出属性名
				var propertyName:String=propertyInfo.@name;

				// 根据属性名来访问
				trace(fromeVO[propertyName]);
			}
			while (_loc_5 in _loc_4)
			{

				accessorXML=_loc_5[_loc_4];
				if (accessorXML.@access != "readwrite")
				{
				}
				if (accessorXML.@access == "readonly")
				{
					propertyNames.push(accessorXML.@name);
				}
			}
			var _loc_4:int=0;
			var _loc_5:*=propertyNames;
			do
			{

				prop=_loc_5[_loc_4];
				try
				{
					if (fromeVO.hasOwnProperty(prop))
					{
						toVO[prop]=fromeVO[prop];
					}
				}
				catch (e:Error)
				{
				}
			} while (_loc_5 in _loc_4)
			return toVO;
		}
		/**
		 * 缓存已经反射过了的对象
		 */
		private var typeCache:Object=new Object();

		/**
		 * 获取类信息
		 * @param target 要获取的目标对象
		 * @return TypeDescriptorEntry 实例
		 *
		 */
		public function describeType(target:*):TypeDescription
		{

			var className:String=getQualifiedClassName(target);

			/**
			 * 检查缓存中是否已经有目标对象项, 如果有就返回缓存中的内容
			 */
			if (className in typeCache)
				return typeCache[className];


			/**
			 * 暂存属性列表
			 */
			var propertyNames:Array=[];


			/**
			 * 获取类信息, 如果传入的是实例则获取实例类型的类信息
			 */
			var typeInfo:XML=flash.utils.describeType(target is Class ? target : getDefinitionByName(className) as Class);


			/**
			 * 获取类中所有的属性和访问器
			 */
			var properties:XMLList=typeInfo.factory..accessor.(@access != "writeonly") + typeInfo..variable;


			/**
			 * 遍历并存放到 propertyNames 中
			 */
			for each (var propertyInfo:XML in properties)
				propertyNames.push(propertyInfo.@name);


			/**
			 * 创建 TypeDescriptorEntry 的实例并把 propertyNames 等属性丢进去
			 */
			var entry:TypeDescription=new TypeDescription();
			entry.name=className;
			entry.properties=propertyNames;
			entry.typeInfo=typeInfo;


			/**
			 * 缓存到 typeCache 中以便下次从缓存中读取
			 */
			typeCache[className]=entry;

			return entry;
		}

		private function testRoomCode():void
		{
			var roomCount:int=101;
			var floorCount:int=roomCount / 5;
			var roomCode:int
			var num:int
			for (var i:int=1; i <= roomCount; i++)
			{
				num=(((i - 1) / floorCount) + 1);
				roomCode=num * 100 + (i - floorCount * (num - 1));
				trace(i + ":" + num + ":" + roomCode);
			}
		}

		private function getroomCode(id:int):void
		{

		}

		private function testReadObject(bytes:ByteArray):Object
		{
			bytes.uncompress();
			bytes.position=0;
			var obj:Object=bytes.readObject();
			trace(obj.id + obj.username + obj.createdate);
//			var info:Info=obj as Info;
//			trace(info.getString());
			return obj;
		}

		private function testWriteObject():ByteArray
		{
			var bytes:ByteArray=new ByteArray();
			bytes.writeObject(new Info());
			printBytes("", bytes);
			bytes.compress();
			printBytes("bytes", bytes);
			return bytes;
		}

		public function printBytes(msg:String, bytes:ByteArray):void
		{
			var result:String=msg + " length: " + bytes.length + " content:";
			for (var i:int=0; i < bytes.length; i++)
			{
				result+=bytes[i].toString(16) + ",";
			}
			result+="]";
			trace(result);
		}

		private function testStopTime():void
		{
//			var sw:StopWatch=new StopWatch();
//			sw.start("test");
//			sw.stop();
//			trace(sw.toString());

		}

		private function requestConnectServer(e:NetEvent):void
		{
			var p:Packet=new Packet();
			p.writeShort(1000);
			p.writeInt(e.playerID);
			trace(e.playerID);
			client.sendPacket(p);
		}

		private function onConnectServerHandler(e:Event):void
		{
			tf.appendText("已建立连接\n");
		}

		private function onReadedDataHandler(e:NetEvent):void
		{
			var p:Packet=new Packet(e.bytesData);
			var info:String="---" + p.readInt() + "---" + p.readInt() + "---" + p.readString() + "\n";
			tf.appendText(info);
		}
	}
}

class TypeDescription
{

	/**
	 * 类的属性列表, 包括访问器 (accessor) 和变量 (variable)
	 */
	public var properties:Array; /* of String */


	/**
	 * 类名
	 */
	public var name:String;



	/**
	 * 由 flash.utils.describeType() 获取的原始 XML 数据
	 */
	public var typeInfo:XML;


	public function TypeDescription()
	{
	}

	public function Copy(from:Object, to:Object):void
	{
		for each (var propertie:* in properties)
		{
			to[propertie]=from[propertie];
		}
	}

	public function traceInfo():void
	{
		trace(name);
		trace(properties);
		trace(typeInfo);
	}
}

class Info extends Object
{
	public var id:int;
	public var username:String;
	public var body:String;
	public var createdate:Date;

	public function Info()
	{
		id=1;
//		username="peng03232323fdfdfadfdfdfdfdfdf";
//		body="hello11111111111111111111111111111111111";	
	}

	public function getString():String
	{
		return id + username + body + createdate;
	}
}
